package com.kexun.servlet.admin;

import com.kexun.pojo.Teacher;
import com.kexun.pojo.User;
import com.kexun.service.AdmService;
import com.kexun.service.impl.AdmServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ServletaddTeacher")
public class ServletaddTeacher extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println(111);
        String teaNum = request.getParameter("teaNum");
        String name = request.getParameter("teaName");
        String password = request.getParameter("password");
        String teaSex = request.getParameter("teaSex");
        String teaAge = request.getParameter("teaAge");
        String teaCourse = request.getParameter("teaCourse");
        String major = request.getParameter("major");
        String department = request.getParameter("department");
        String phone = request.getParameter("phone");
        User user = new User(teaNum,name,password,phone,1);
        Integer Num = Integer.valueOf(teaNum);
        Integer Age = Integer.valueOf(teaAge);
        Teacher teacher = new Teacher(Num,name,teaSex,Age,teaCourse,major,department);

        AdmService as = new AdmServiceImpl();
        int i =  as.addTeacher(user,teacher);
        if(i>0){
            response.getWriter().print("添加成功");
            response.setHeader("refresh","1;url=admin/teacherAllInfo.jsp");
        }else{
            response.getWriter().print("添加失败");
            request.getRequestDispatcher("admin/addTeacher.jsp").forward(request,response);
        }



    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
